/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.niclab.vanbus.R;
import org.niclab.vanbus.model.SkyTrainLine;
import org.niclab.vanbus.model.SkyTrainLine.SkyTrainRoute;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.TabActivity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SkyTrainTab {
	private static final String LOG_TAG="SkyTrainTab";
	
	private TabActivity tabActivity;
	private Context context;
	private TextView dayText;
	private TableLayout table;
	private List<SkyTrainLine> skyTrainLines;


	private String selectedDay;
	public SkyTrainTab(TabActivity tabActivity){
		this.tabActivity = tabActivity;
		this.context = tabActivity.getApplicationContext();
		TabHost tabHost = tabActivity.getTabHost();
		TabHost.TabSpec spec = tabHost.newTabSpec(tabActivity.getString(R.string.skytrain_tab_title)).setIndicator(tabActivity.getString(R.string.skytrain_tab_title)).setContent(R.id.skytrain_tab);
		tabHost.addTab(spec);
		

		
	}
	
	private void initialzeDayButtons(){
		Button buttonBack = (Button) tabActivity.findViewById(R.id.skytrain_day_button_backward);
		dayText = (TextView) tabActivity.findViewById(R.id.skytrain_day_text);
		buttonBack.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				selectedDay = SkyTrainLine.SkyTrainRoute.prevDay(selectedDay);
				dayText.setText(selectedDay);
				repaint();

				
			}
			
		});
		
		Button buttonForward = (Button) tabActivity.findViewById(R.id.skytrain_day_button_forward);
		
		buttonForward.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				selectedDay = SkyTrainLine.SkyTrainRoute.nextDay(selectedDay);
				dayText.setText(selectedDay);
				repaint();

				
			}
			
		});
		
	}
	
	

	public void repaint(){
		int index=0;
		for(SkyTrainLine trainLine:skyTrainLines){
			index++;
			for(SkyTrainRoute route : trainLine.getRoutes()){
				View view =table.getChildAt(index++);
				TextView first = (TextView) view.findViewById(R.id.skytrain_route_time_first);
				first.setText(route.getFirstShiftTime(selectedDay));
				
				TextView last = (TextView) view.findViewById(R.id.skytrain_route_time_last);
				last.setText(route.getLastShiftTime(selectedDay));
			
			}
			
			
		}
		
	}
	
	public void initScheduleTable(ViewGroup rootView){
		Log.d(LOG_TAG, "initializing the SkyTrain schedule table");

		try {
			skyTrainLines = readSkyTrainXML();
			
			int i=0;
			LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			for(SkyTrainLine trainLine:skyTrainLines){
				
			
				
				View lineRow= inflator.inflate(R.layout.skytrain_line, null);
				TextView title = (TextView) lineRow.findViewById(R.id.skytrain_line_name);			
				title.setText(trainLine.getName());

				rootView.addView(lineRow, i++);

				
				for(SkyTrainRoute route : trainLine.getRoutes()){
					
					
					
					View routeView = rootView.inflate(context, R.layout.skytrain_route,null);
					
					TextView  routeName= (TextView) routeView.findViewById(R.id.skytrain_route_name);
					routeName.setText(route.getStart()+" -> "+route.getEnd());
					
					TextView routeFirstTime = (TextView) routeView.findViewById(R.id.skytrain_route_time_first);
					routeFirstTime.setText(route.getFirstShiftTime(selectedDay));

					
					TextView routeLastTime = (TextView) routeView.findViewById(R.id.skytrain_route_time_last);
					routeLastTime.setText(route.getLastShiftTime(selectedDay));
					
					rootView.addView(routeView, i++);
	
				
	

				}
			
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}
	
	public void onCreate(){
		selectedDay = SkyTrainLine.SkyTrainRoute.curDay();
		table= (TableLayout) tabActivity.findViewById(R.id.skytrain_schedule_table);
		initialzeDayButtons();
		initScheduleTable(table);
	}
	
	
	
	private List<SkyTrainLine> readSkyTrainXML() throws XmlPullParserException, IOException{
		XmlResourceParser xmlParser = tabActivity.getResources().getXml(R.xml.skytrain);
		List<SkyTrainLine> skyTrains = new ArrayList<SkyTrainLine>();
		
		int eType= xmlParser.getEventType();
		

		SkyTrainLine skyTrainLine = null;
		SkyTrainRoute route = null;

		while(eType != XmlPullParser.END_DOCUMENT){
			
			
			switch(eType){
			case XmlPullParser.START_TAG:
				//Log.v(LOG_TAG,"start tag name:" + xmlParser.getName());
				String tagName = xmlParser.getName();
				if(tagName.equals("trainline")){
				
					skyTrainLine = new SkyTrainLine(xmlParser.getAttributeValue(null, "name"));
					skyTrains.add(skyTrainLine);
				}
				
				if(tagName.equals("route")){
					route = new SkyTrainRoute();
					String startPlace = xmlParser.getAttributeValue(null, "start");
					String endPlace = xmlParser.getAttributeValue(null, "end");
					
					route.setStart(startPlace);
					route.setEnd(endPlace);
					skyTrainLine.addSkyTrainRoute(route);
					
					
				}

				
				if(tagName.equals("time")){
					
					String day = xmlParser.getAttributeValue(null, "day");
							
					String first = xmlParser.getAttributeValue(null, "first");
					String last = xmlParser.getAttributeValue(null, "last");
					
					String[] times = {first, last};
					
					route.addTime(day, times);
					
				}
				
				
				break;
				
				
			case XmlPullParser.END_TAG:
				//Log.v(LOG_TAG, "closing tag name:" + xmlParser.getName());
				
				break;
			
			
			
			}
			
			eType = xmlParser.next();
		}
		return skyTrains;
		
	}
	
	
	

}
